package cn.shuangliang.contract.web;

import cn.shuangliang.contract.model.Product;
import cn.shuangliang.contract.service.ProductService;
import cn.shuangliang.contract.util.StrUtil;
import cn.shuangliang.contract.vo.Page;
import cn.shuangliang.contract.vo.Result;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * @author oldliu
 * @since 1.0
 */
public class ProductController {
    ProductService productService=new ProductService();

    public String index(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int p= StrUtil.toInt(request.getParameter("pageNo"),1);
        String style= StrUtil.trim(request.getParameter("style"));
        String name= StrUtil.trim(request.getParameter("pname"));

        Page<Product> page = productService.search(p, name, style);
        request.setAttribute("p",page);
        request.setAttribute("pname",name);
        request.setAttribute("style",style);
        return "/WEB-INF/views/products.jsp";
        //return "/WEB-INF/views/products.html";
    }
    public String toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return "/WEB-INF/views/addproduct.html";
    }
    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String style= StrUtil.trim(request.getParameter("style"));
        String sidu= StrUtil.trim(request.getParameter("sidu"));
        String name= StrUtil.trim(request.getParameter("pname"));
        String desc= StrUtil.trim(request.getParameter("desc"));
        Float pr=StrUtil.toFloat(request.getParameter("price"));

        Product p=new Product(name,sidu,style,pr,desc);
        p.setAddtime(new java.util.Date() );
        int r = this.productService.add(p);

        response.sendRedirect("p-index.do?msg="+ URLEncoder.encode(r>0?"增加成功":"增加失败","utf-8"));
    }
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Long id=StrUtil.toLong(request.getParameter("id"));
        String style= StrUtil.trim(request.getParameter("style"));
        String sidu= StrUtil.trim(request.getParameter("sidu"));
        String name= StrUtil.trim(request.getParameter("pname"));
        String desc= StrUtil.trim(request.getParameter("desc"));
        Float pr=StrUtil.toFloat(request.getParameter("price"));

        Product p=new Product(name,sidu,style,pr,desc);
        p.setId(id);
        //p.setAddtime(new java.util.Date() );
        int r = this.productService.update(p);

        response.sendRedirect("p-index.do?msg="+ URLEncoder.encode(r>0?"修改成功":"修改失败","utf-8"));
    }
    public String toUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Long id=StrUtil.toLong(request.getParameter("id"));
        request.setAttribute("product",this.productService.get(id));
        return "/WEB-INF/views/updateproduct.html";
    }
    public Result get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Long id=StrUtil.toLong(request.getParameter("id"));
        Product product = this.productService.get(id);
        return product==null?Result.error("对象不存在"):Result.success(product);
    }
    public void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Long id=StrUtil.toLong(request.getParameter("id"));

        //p.setAddtime(new java.util.Date() );
        int r = this.productService.del(id);

        response.sendRedirect("p-index.do?msg="+ URLEncoder.encode(r>0?"删除成功":"删除失败","utf-8"));
    }
}
